﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Admin;
using System.Data;
using BO;

namespace ProjectQuaDep.Admin.Comments
{
    public partial class Comment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowAll();
            }
        }

        protected void ShowAll()
        {
            string search = txtSearch.Text.Trim();
            DataTable dt = new DataTable();
            dt = CommentBO.GetAllCommentBO(search, ddlType.SelectedValue.ToString(), rbStatus.SelectedValue.ToString());
            if (dt.Rows.Count > 0)
            {
                gvShow.DataSource = dt;
                gvShow.DataBind();
                int countCM = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][Const.tbComments.Status].ToString() == "0")
                    {
                        countCM++;
                    }
                }
                if (countCM == 0)
                {
                    lblCheckComment.Text = "";
                }
                else
                {
                    lblCheckComment.Text ="Có "+ countCM.ToString() + " comment chưa được duyệt!";
                }
            }
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            //cap nhật
            int kq = CommentBO.EditConmentCommentBO(lblID.Text, txtContent.Text, txtReply.Text);
            if (kq != 1)
            {
                lblMs.Text = "Cập nhật thất bại!";
            }
            else
            {
                pnUpdate.Visible = false;
                lblMs.Text = "";
                ShowAll();
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void ibSearch_Click(object sender, ImageClickEventArgs e)
        {
            ShowAll();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdRemove")
            {
                //xóa
                int kq = CommentBO.DeleteCommentBO(ID);
                if (kq != 1)
                {
                    lblPage.Text = "Xóa thất bại!";
                }
                else
                {
                    lblPage.Text = "Xóa thành công!";
                    ShowAll();
                }
            }
            else if (cmdName == "cmdStatus")
            {
                //xóa
                int kq = CommentBO.StatusCommentBO(ID, "1");
                if (kq != 1)
                {
                    lblPage.Text = "Duyệt thất bại!";
                }
                else
                {
                    lblPage.Text = "Duyệt thành công!";
                    ShowAll();
                }
            }
            else
            {
                pnUpdate.Visible = true;
                DataTable dt = CommentBO.GetInfomationByIDBO(ID);
                if (dt.Rows.Count > 0)
                {
                    txtContent.Text = dt.Rows[0][Const.tbComments.Content].ToString();
                    txtReply.Text = dt.Rows[0][Const.tbComments.Reply].ToString();
                    lblID.Text = ID;
                }
            }
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            ShowAll();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAll();
        }

        protected void rbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAll();
        }
    }
}